package com.shm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SystemMessage;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.vo.SystemMessageVo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.shm.service.ISystemMessageService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统消息Controller
 *
 * @author dam
 * @date 2023-07-21
 */
@Api("系统消息管理")
@RestController
@RequestMapping("/market/message")
public class SystemMessageController extends BaseController {
    @Autowired
    private ISystemMessageService systemMessageService;

    /**
     * 查询系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('market:message:list')")
    @GetMapping("/list")
    @ApiOperation("获取消息列表")
    public TableDataInfo list(SystemMessage systemMessage) {
        startPage();
        List<SystemMessage> list = systemMessageService.selectSystemMessageList(systemMessage);
        return getDataTable(list);
    }

    /**
     * 查询系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('market:message:list')")
    @PostMapping("/listVo")
    @ApiOperation("获取消息列表")
    public TableDataInfo listVo(@RequestBody SystemMessageVo systemMessageVo) {
        startPage();
        List<SystemMessageVo> list = systemMessageService.selectSystemMessageVoList(systemMessageVo);
        return getDataTable(list);
    }

    /**
     * 导出系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('market:message:export')")
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemMessage systemMessage) {
        List<SystemMessage> list = systemMessageService.selectSystemMessageList(systemMessage);
        ExcelUtil<SystemMessage> util = new ExcelUtil<SystemMessage>(SystemMessage.class);
        util.exportExcel(response, list, "系统消息数据");
    }

    /**
     * 获取系统消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(systemMessageService.selectSystemMessageById(id));
    }

    /**
     * 新增系统消息
     */
    @PreAuthorize("@ss.hasPermi('market:message:add')")
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SystemMessage systemMessage) {
        LoginUser loginUser = getLoginUser();
        systemMessage.setEditorId(loginUser.getUserId());
        return toAjax(systemMessageService.insertSystemMessage(systemMessage));
    }

    /**
     * 修改系统消息
     */
    @PreAuthorize("@ss.hasPermi('market:message:edit')")
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SystemMessage systemMessage) {
        return toAjax(systemMessageService.updateSystemMessage(systemMessage));
    }

    /**
     * 删除系统消息
     */
    @PreAuthorize("@ss.hasPermi('market:message:remove')")
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(systemMessageService.deleteSystemMessageByIds(ids));
    }
}
